package com.gobo.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobo.commonutils.JwtUtils;
import com.gobo.commonutils.R;
import com.gobo.eduservice.pojo.EduComment;
import com.gobo.eduservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/eduservice/comment")
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    //普通用户分页查询评论信息
    @PostMapping("front/page/{current}/{size}")
    public R getFrontPage(@RequestParam String courseId,@PathVariable long current,@PathVariable long size){
        Page<EduComment> page = new Page<>(current,size);
        Map<String,Object> map = commentService.getFrontPageByCourseId(page,courseId);
        return R.ok().data(map);
    }

    //添加评论信息
    @PostMapping("front/save")
    public R saveFrontComment(@RequestBody EduComment comment, HttpServletRequest request){
        boolean b = JwtUtils.checkToken(request);
        if (!b){
            return R.error().code(30001).message("用户未登录");
        }
        if (StringUtils.isEmpty(comment.getContent())){
            return R.error().message("参数非法【评论内容为空！】");
        }

        String MemberId = JwtUtils.getMemberIdByJwtToken(request);
        Boolean isSave = commentService.saveEduCommentContainsMemberInfo(comment,MemberId);
        return isSave?R.ok():R.error();
    }



}

